package com.company.eduorder.controller;


import com.company.eduorder.entity.Order;
import com.company.eduorder.service.OrderService;
import com.company.utils.JwtUtils;
import com.company.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-04-23
 */
@RestController
@RequestMapping("/eduorder/order")
@CrossOrigin
public class OrderController {

    @Resource
    private OrderService orderService;

    //购买课程生成订单
    @PostMapping("/createOrder/{courseId}")
    public R saveOrder(@PathVariable String courseId, HttpServletRequest request) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        String orderId = orderService.saveOrder(courseId, id);
        return R.ok().data("orderId", orderId);
    }

    //根据订单ID查询
    @GetMapping("/gerOrderInfo/{orderId}")
    public R gerOrderInfo(@PathVariable String orderId) {
        Order order = orderService.getById(orderId);
        return R.ok().data("item", order);
    }

    //根据用户ID和课程ID查询是否购买非免费课程
    @GetMapping("/isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable(required = false, name = "courseId") String courseId,
                               @PathVariable(required = false, name = "memberId") String memberId) {
        return orderService.isBuyCourse(courseId, memberId);
    }

}

